var href = decodeURI(location.href)
    console.log(href);
    function getQueryString(url){
        //通过？分割数组
        let cs = url.split("?")[1]
        console.log(cs);
    
        //判断如果没有参数，返回null
        if(!cs){
            return null;
        }
        
        //针对所有参数分割成数组
        let csarr = cs.split("&")
        console.log(csarr);
    
        //定义一个接受结果的对象
        let csobj = {};
        //遍历所有的参数数组，通过=将key和value分割存入对象
        csarr.forEach(item => {
            csobj[item.split("=")[0]] = item.split("=")[1];
        });
        console.log(csobj);
        return csobj;
    }
    var cs = getQueryString(href);
    console.log(cs);

    var main_top = document.querySelector(".main_top")
    var main_link = document.querySelector(".main_link")
    var comment = document.querySelector(".comment")
fetch("http://chst.vip:1234/api/getproduct?productid="+cs.productId)
.then(data=>{
    return data.json()
})
.then(res=>{
    console.log(res);
    console.log(res.result);
    let ele = "";
    ele += ` ${res.result[0].productImg}
                <p>${res.result[0].productName}</p>
                <div class="top_text">
                    <div class="text_half">
                        <span>当前最低</span>
                        <span>￥6000</span>
                    </div>
                    <div class="text_half">
                        <span>优选评论</span>
                        <span>8266条</span>
                    </div>
                </div>
            `
    main_top.innerHTML = ele
    main_link.innerHTML = res.result[0].bjShop
})

var one = document.querySelector(".one")
// one.innerText = 




const xhr2 = new XMLHttpRequest();
xhr2.open("get","http://chst.vip:1234/api/getproductcom?productid="+cs.productId)
xhr2.send();
xhr2.onload = function(){
    let res = JSON.parse(xhr2.responseText);
    console.log(res);
    console.log(res.result);
    let ele2 = ""
    res.result.forEach(item=>{
        ele2 += `<div class="comment">
                    <div class="comment_top">
                        <p>${item.comName}</p>
                        <span>${item.comTime}</span>
                    </div>
                    <div class="comment_text">
                        <p>${item.comContent}</p>
                    </div>
                    <div class="comment_from">
                        <p>${item.comFrom}</p>
                    </div>
                </div>`
    })
    comment.innerHTML = ele2
}